package com.chuzhiyun.chengguoxy.feature.internship;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chuzhiyun.chengguoxy.R;
import com.chuzhiyun.chengguoxy.databinding.FragmentInternshipBinding;
import com.chuzhiyun.chengguoxy.feature.base.BaseFragment;
import com.chuzhiyun.chengguoxy.feature.bean.InternshipProjectBean;
import com.chuzhiyun.chengguoxy.feature.bean.internshipTaskBean;
import com.chuzhiyun.chengguoxy.feature.internship.clockin.ClockHomeActivity;
import com.chuzhiyun.chengguoxy.feature.internship.leave.LeaveHomeActivity;
import com.chuzhiyun.chengguoxy.feature.internship.project.ProjectListActivity;
import com.chuzhiyun.chengguoxy.feature.internship.worktags.NavAdapterViewHolder;
import com.chuzhiyun.chengguoxy.feature.internship.worktags.WorkTagsHomeActivity;
import com.chuzhiyun.chengguoxy.feature.internship.worktags.WorkTagsUtlis;
import com.chuzhiyun.chengguoxy.global.UserConfig;
import com.chuzhiyun.chengguoxy.network.NetApi;
import com.chuzhiyun.chengguoxy.network.NetService;
import com.chuzhiyun.chengguoxy.network.StringNetCallback;
import com.chuzhiyun.chengguoxy.utlis.CustomClickListener;
import com.chuzhiyun.chengguoxy.utlis.NewItemClickListener;
import com.chuzhiyun.chengguoxy.utlis.StringUtils;
import com.chuzhiyun.chengguoxy.utlis.Utils;
import com.google.gson.Gson;
import com.zaaach.transformerslayout.TransformersOptions;
import com.zaaach.transformerslayout.holder.Holder;
import com.zaaach.transformerslayout.holder.TransformersHolderCreator;
import com.zaaach.transformerslayout.listener.OnTransformersItemClickListener;

import java.util.ArrayList;
import java.util.List;

import static java.util.Objects.requireNonNull;

@SuppressWarnings({"FieldCanBeLocal", "FieldMayBeFinal"})
public class InternshipFragment extends BaseFragment<FragmentInternshipBinding> {

    private List<internshipTaskBean> navList = new ArrayList<>();
    private InternshipFragment.MyAdapter myAdapter;

    @Override
    public void initView(View view) {

    }

    @Override
    public void initData() {

    }

    @Override
    public void setListener() {
        getInternship();
        getTasksInfo();
        binding.changeText.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                Intent intent = new Intent();
                intent.setClass(getContext(),ProjectListActivity.class);
                startActivity(intent);
            }
        });
    }

    private void getInternship(){
        String url = NetApi.getInternshipsList();
        NetService.getInstance().get(url, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                System.out.println(response);
            }

            @Override
            public void onError(String error) {
                System.out.println(error);
            }
        });
    }

    private void getTasksInfo(){
        String url = NetApi.getInternshipProject("1");
        NetService.getInstance().get(url, new StringNetCallback() {
            @Override
            public void onSuccess(String response, int statusCode) throws Exception {
                System.out.println(response);
                if (statusCode==200){
                    Gson gson = new Gson();
                    InternshipProjectBean bean = gson.fromJson(response,InternshipProjectBean.class);
                    System.out.println(bean.getName());
                    List<internshipTaskBean> addNavList = new ArrayList<>();
                    for (int i = 0;i<bean.getTasks().size();i++){
                        internshipTaskBean taskBean = new internshipTaskBean();
                        taskBean.setId(bean.getTasks().get(i).getId());
                        taskBean.setName(bean.getTasks().get(i).getName());
                        taskBean.setLogoId(bean.getTasks().get(i).getId());
                        taskBean.setShowRed(false);
                        addNavList.add(taskBean);
                    }
                    createTagView(addNavList);
                }
            }

            @Override
            public void onError(String error) {
                System.out.println(error);
            }
        });
    }

    private void createTagView(List<internshipTaskBean> addNavList) {
        List<internshipTaskBean>normalArray = new ArrayList<>();
        internshipTaskBean bean0 = new internshipTaskBean();
        bean0.setName("打卡");
        bean0.setId(1);
        bean0.setLogoId(1);
        bean0.setNavTypeStr("1");
        normalArray.add(bean0);

        internshipTaskBean bean1 = new internshipTaskBean();
        bean1.setName("请假");
        bean1.setId(2);
        bean1.setLogoId(2);
        bean1.setNavTypeStr("2");
        normalArray.add(bean1);

        internshipTaskBean bean2 = new internshipTaskBean();
        bean2.setName("文件");
        bean2.setId(3);
        bean2.setLogoId(3);
        bean2.setNavTypeStr("3");
        normalArray.add(bean2);

        internshipTaskBean bean3 = new internshipTaskBean();
        bean3.setName("成绩");
        bean3.setId(4);
        bean3.setLogoId(4);
        bean3.setNavTypeStr("4");
        normalArray.add(bean3);

        navList.addAll(normalArray);

        if (addNavList!=null)
        {
            navList.addAll(addNavList);
        }

        internshipTaskBean bean4 = new internshipTaskBean();
        bean4.setName("离职");
        bean4.setId(5);
        bean4.setLogoId(5);
        bean4.setNavTypeStr("5");
        navList.add(bean4);

        binding.tagsRecyclerView.setLayoutManager(new GridLayoutManager(getContext(),4));
        myAdapter = new MyAdapter(R.layout.item_worktags, navList);
        binding.tagsRecyclerView.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new NewItemClickListener() {
            @Override
            protected void onSingleClick(BaseQuickAdapter adapter, View view, int position) {
                String tasksId = navList.get(position).getId()+"";
                System.out.println(tasksId);
                String typeStr = navList.get(position).getNavTypeStr();
                if (StringUtils.isEmpty(typeStr))
                {
                    Intent intent = new Intent(getContext(), WorkTagsHomeActivity.class);
                    intent.putExtra("tasksId", tasksId);
                    intent.putExtra("tasksName",navList.get(position).getName());
                    startActivity(intent);
                }
                else
                {
                    if (typeStr.equals("1")){
                        //打卡
                        Intent intent = new Intent(getContext(), ClockHomeActivity.class);
                        startActivity(intent);
                    }
                    if (typeStr.equals("2")){
                        Intent intent = new Intent(getContext(), LeaveHomeActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });
    }

    static class MyAdapter extends BaseQuickAdapter<internshipTaskBean, BaseViewHolder> {
        MyAdapter(int layoutResId, List<internshipTaskBean> data) {
            super(layoutResId, data);
        }

        @SuppressLint("ResourceType")
        @Override
        protected void convert(BaseViewHolder helper, internshipTaskBean item) {
            helper.setText(R.id.tv_menu_text,item.getName());
            if (StringUtils.isEmpty(item.getNavTypeStr())) {
                helper.setGone(R.id.tags_Img,true);
                if (item.getLogoId()==0)
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.task_item0);
                }
                if (item.getLogoId()==1)
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.task_item1);
                }
                if (item.getLogoId()==2)
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.task_item2);
                }
                if (item.getLogoId()==3)
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.task_item3);
                }
                if (item.getLogoId()==4)
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.task_item4);
                }
                if (item.getLogoId()==5)
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.task_item5);
                }
                if (item.getLogoId()==6)
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.task_item6);
                }
                if (item.getLogoId()==7)
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.task_item7);
                }
                if (item.getLogoId()==8)
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.task_item8);
                }
                if (item.getLogoId()==9)
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.task_item9);
                }
                if (item.getLogoId()==10)
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.task_item10);
                }
            }
            else {
                if (item.getNavTypeStr().equals("1"))
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.work_item0);
                    if (item.isShowRed()){
                        helper.setVisible(R.id.tags_Img,true);
                    }else {
                        helper.setGone(R.id.tags_Img,true);
                    }
                }
                if (item.getNavTypeStr().equals("2"))
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.work_item1);
                    helper.setGone(R.id.tags_Img,true);
                }
                if (item.getNavTypeStr().equals("3"))
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.work_item2);
                    helper.setGone(R.id.tags_Img,true);
                }
                if (item.getNavTypeStr().equals("4"))
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.work_item3);
                    helper.setGone(R.id.tags_Img,true);
                }
                if (item.getNavTypeStr().equals("5"))
                {
                    helper.setImageResource(R.id.iv_menu_icon,R.mipmap.work_item4);
                    helper.setGone(R.id.tags_Img,true);
                }
            }
        }
    }
}